package dataSource

import (
	"gitee.com/zhucheer/orange/queue"
	"github.com/Shopify/sarama"
	"github.com/gomodule/redigo/redis"
	"gorm.io/gorm"
	"gorm.io/driver/mysql"
	goRedis "github.com/go-redis/redis/v7"
)

var (
	MysqlDb *gorm.DB
	RedisConn redis.Conn
	GoRedisClint *goRedis.Client
	MqProducer queue.MqProducer
	KafkaProducer sarama.SyncProducer
	err error
)

func init()  {
	dsn := "root:new_password@tcp(127.0.0.1:3306)/we_chat?charset=utf8mb4&parseTime=True&loc=Local"
	MysqlDb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil{
		panic("db error")
	}

	pool := &redis.Pool{
		MaxIdle : 10,
		MaxActive : 0,
		IdleTimeout : 100,
		Dial : func()(redis.Conn, error){
			return redis.Dial("tcp", "127.0.0.1:6379")
		},
	}
	RedisConn = pool.Get()

	GoRedisClint = goRedis.NewClient(&goRedis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	_, err = GoRedisClint.Ping().Result()
	if err != nil {
		panic("redis error")
	}

	MqProducer = queue.RegisterRocketProducerMust([]string{"127.0.0.1:9876"}, "test", 1)

	config := sarama.NewConfig()
	config.Producer.RequiredAcks = sarama.WaitForAll
	config.Producer.Partitioner = sarama.NewRandomPartitioner
	config.Producer.Return.Successes = true

	KafkaProducer, err = sarama.NewSyncProducer([]string{"127.0.0.1:9092"}, config)
	if err != nil {
		panic("kafka error")
	}
}


